let fs = require("fs")
let fileChecker = require("./file-checker")
let typeChecker = require("./file-type")
let { resolve } = require("path")

module.exports = function(target_dir, callback) {
  let checked_files = {}

  try {
    fs.readdir(target_dir, (err, files_list) => {
      if (!err) {
        // create an object that contains the info of the files of target directory
        checked_files.list = files_list.map(item => {
          let single_file = {
            name: item,
            type: typeChecker(resolve(target_dir, item))
          }

          if (single_file.type === "FILE")
            single_file.md5_value = fileChecker(resolve(target_dir, item))

          return single_file
        })

        checked_files.count = checked_files.list.length

        callback(checked_files)
      }
    })
  } catch (e) {
    console.error("error when try to read directory: " + target_dir)
  }
}